#!/bin/env python
# coding:utf-8

import redis
from generate_rediskey import login_key, pay_key
from datahelper import calculation_time



class RedisHelper(object):
    _pool = None
    _conn = None
    _pipeline = None


    def __init__(self, redis_confg):
        self._pool = redis.ConnectionPool(
            host=redis_confg['host'],
            port=redis_confg['port'])

        self._conn = redis.Redis(connection_pool=self._pool)
        self._pipeline = self._conn.pipeline()


    # 计算留存
    def ordinary_user_retained(self, select_day, main_channel, sub_channel, game_id):


        two_day = calculation_time.add_day(select_day, 1)
        three_day = calculation_time.add_day(select_day, 2)
        seven_day = calculation_time.add_day(select_day, 6)


        select_day_result = login_key(select_day, main_channel, sub_channel, game_id)
        two_day_calculation_result = login_key(two_day, main_channel, sub_channel, game_id)
        three_day_calculation_result = login_key(three_day, main_channel, sub_channel, game_id)
        seven_day_calculation_result = login_key(seven_day, main_channel, sub_channel, game_id)


        # 查找指定时间内新增用户
        select_day_user_key = select_day_result['new_user_key']


        # 查找指定时间内的活跃用户
        two_day_user_key = two_day_calculation_result['login_user_key']
        three_day_user_key = three_day_calculation_result['login_user_key']
        seven_day_user_key = seven_day_calculation_result['login_user_key']


        # 当指定时间内的新增用户在其后面的指定日期还有登陆,则判定其为留存用户
        select_day_usercount = self._conn.scard(select_day_user_key)
        two_day_retained = float(self._conn.sinter(select_day_user_key, two_day_user_key)) / float(select_day_usercount)
        three_day_retained = float(self._conn.sinter(select_day_user_key, three_day_user_key)) / float(select_day_usercount)
        seven_day_retained = float(self._conn.sinter(select_day_user_key, seven_day_user_key)) / float(select_day_usercount)

        retained_result = {
            'two_day_retained': two_day_retained,
            'three_day_retained': three_day_retained,
            'seven_day_retained': seven_day_retained
        }

        return retained_result


    # 计算15日LTV,30日LTV
    def pay_user_retained(self, select_day, main_channel, sub_channel, game_id):


        # 定义查询15日和30日后的数据
        fifteen_day = calculation_time.add_day(select_day, 15)
        thirty_day = calculation_time.add_day(select_day, 30)

        select_day_result = pay_key(select_day, main_channel, sub_channel, game_id)
        fifteen_day_result = pay_key(fifteen_day, main_channel, sub_channel, game_id)
        thirty_day_resulit = pay_key(thirty_day, main_channel, sub_channel, game_id)


        # 查找指定时间内新增用户
        select_day_pay_user_key = select_day_result['pay_new_user_key']


        # 查找指定时间内的活跃用户
        fifteen_day_pay_user_key = fifteen_day_result['pay_user_key']
        thirty_day_pay_user_key = thirty_day_resulit['pay_user_key']


        # 查看指定日期的付费用户是否还在付费
        select_day_usercount = self._conn.scard(select_day_pay_user_key)
        fifteen_day_pay_user_retained = float(self._conn.sinter(fifteen_day_pay_user_key, select_day_pay_user_key)) / float(select_day_usercount)
        thirty_day_pay_user_retained = float(self._conn.sinter(thirty_day_pay_user_key, select_day_pay_user_key)) / float(select_day_usercount)


        pay_user_retained_result = {
            'fifteen_day_pay_user_retained': fifteen_day_pay_user_retained,
            'thirty_day_pay_user_retained': thirty_day_pay_user_retained
        }

        return pay_user_retained_result


    # 计算出指定日期的相关参数,新增用户,活跃用户,当日收入,付费用户数,付费金额,arpu,arppu,新增玩家的付费人数,新增玩家的付费金额,今日新增用户付费率,
    def select_time_result(self, select_time, main_channel, sub_channel, game_id):


        # 由于查看的指定日期的新增和日活,所以2个时间是一致的
        user_key = login_key(select_time, main_channel, sub_channel, game_id)
        pay_key_result = pay_key(select_time, main_channel, sub_channel, game_id)

        # 获取所需有的key
        new_user_key = user_key['new_user_key']
        login_user_key = user_key['login_user_key']
        pay_user_key = pay_key_result['pay_user_key']
        pay_money_key = pay_key_result['pay_money_key']
        pay_succorder_key = pay_key_result['pay_succorder_key']
        pay_new_user_key = pay_key_result['pay_new_user_key']
        pay_new_money_key = pay_key_result['pay_new_money_key']
        pay_new_succorder_key = pay_key_result['pay_new_succorder_key']


        new_user_count = self._conn.scard(new_user_key)
        login_user_count = self._conn.scard(login_user_key)
        pay_user_count = self._conn.scard(pay_user_key)
        pay_money = self._conn.get(pay_money_key)
        pay_succorder_count = self._conn.scard(pay_succorder_key)
        pay_new_user_count = self._conn.scard(pay_new_user_key)
        pay_new_money = self._conn.get(pay_new_money_key)
        pay_new_succorder_count = self._conn.scard(pay_new_succorder_key)

        # 所有付费中新增用户付费占比是多少
        new_user_pay_ratio = float(pay_new_user_count) / float(pay_user_count)

        # 活跃用户中的付费用户占比
        login_user_pay_ratio = float(pay_user_count) / float(login_user_count)

        # arpu
        game_arpu = float(pay_money) / float(new_user_count)

        # arppu
        game_arppu = float(pay_money) / float(pay_user_count)





        result_data = {
            'new_user_count': new_user_count,
            'login_user_count': login_user_count,
            'pay_user_count': pay_user_count,
            'pay_money': pay_money,
            'pay_succorder_count': pay_succorder_count,
            'pay_new_user_count': pay_new_user_count,
            'pay_new_money': pay_new_money,
            'pay_new_succorder_count': pay_new_succorder_count,
            'new_user_pay_ratio': new_user_pay_ratio,
            'login_user_pay_ratio': login_user_pay_ratio,
            'game_arpu': game_arpu,
            'game_arppu': game_arppu
        }

        return result_data


    # 统计月活(MAU)
    def active_data(self, select_month, main_channel, sub_channel, game_id):


        select_month_day_keys = '*' + select_month + '*' + ':' + main_channel + '_' + sub_channel + '_' + game_id + ':' + 'loginuser'

        select_days = self._conn.keys(select_month_day_keys)
        game_mau = self._conn.sinter(select_days)

        return game_mau
